import { useEffect, useState } from "react"
import { throttle } from "underscore"

function useScrollPosition() {
  // 记录滚动的状态
  const [scrollX, setScrollX] = useState(0)
  const [scrollY, setScrollY] = useState(0)
  
  // 监听滚动
  useEffect(() => {
    const handleScore = throttle(function scrollfunction() {
      setScrollX(window.scrollX)
      setScrollY(window.scrollY)
    }, 200)

    window.addEventListener("scroll", handleScore)
    // 解除事件监听
    return () => {
      window.removeEventListener(handleScore)
    }
  }, [])
  return { scrollX, scrollY }
}

export default useScrollPosition
